package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.enums.FarmsEnum;
import com.dongjiantong.user.mapper.FarmsMapper;
import com.dongjiantong.user.model.Farms;
import com.dongjiantong.user.model.dtos.FarmsDto;
import com.dongjiantong.user.service.FarmsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author cao
 * @create 2017-11-15 下午3:10
 **/

@Slf4j
@Service(version = "1.0.0")
public class FarmsServiceImpl extends ServiceMybatis<Farms> implements FarmsService {

    @Resource
    private FarmsMapper farmsMapper;

    @Reference(version = "1.0.0")
    private FarmStockService farmStockService;

    @Override
    public PageInfo<Farms> getFarmsListByPage(Integer pageNum, Integer pageSize, Farms farms) {
        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(Farms.class);
        Example.Criteria criteria = example.createCriteria();


        //搜索

        //类型
        Integer farmsType = farms.getFarmsType();
        criteria.andEqualTo("farmsType", farmsType);

        //省
        Integer provinceId = farms.getProvinceId();
        criteria.andEqualTo("provinceId", provinceId);
        //市
        Integer cityId = farms.getCityId();
        criteria.andEqualTo("cityId", cityId);
        //区
        Integer regionId = farms.getRegionId();
        criteria.andEqualTo("regionId", regionId);

        Integer streetId = farms.getStreetId();
        criteria.andEqualTo("streetId", streetId);

        Integer villageId = farms.getVillageId();
        criteria.andEqualTo("villageId", villageId);


        //动物种类
        Integer animalOneType = farms.getAnimalOneType();
        if (animalOneType == null) {
            animalOneType = 0;
        }

        if (animalOneType > 0) {
            criteria.andEqualTo("animalOneType", animalOneType);
        }
        Integer animalTwoType = farms.getAnimalTwoType();
        if (animalTwoType == null) {
            animalTwoType = 0;
        }
        if (animalTwoType > 0) {
            criteria.andEqualTo("animalTwoType", animalTwoType);
        }


        //联系人
        String linkman = farms.getLinkman();
        if (!StringUtil.isBlank(linkman)) {
            criteria.andLike("linkman", "%" + linkman + "%");
        }

        //手机
        String phone = farms.getPhone();
        if (!StringUtil.isBlank(phone)) {
            criteria.andLike("phone", "%" + phone + "%");
        }

        //养殖场名称
        String farmsname = farms.getFarmsname();
        if (!StringUtil.isBlank(farmsname)) {
            criteria.andLike("farmsname", "%" + farmsname + "%");
        }

        //养殖场 id
        Long id = farms.getId();
        if (!StringUtil.isBlank(id)) {
            criteria.andEqualTo("id", id);
        }
        //是否删除
        if (farms.getDeleteFlag() != null) {
            Integer deleteFlag = farms.getDeleteFlag();
            if (deleteFlag == 1) {
                criteria.andEqualTo("deleteFlag", deleteFlag);
            }
        }

        example.setOrderByClause("created_at desc");

        List<Farms> farmsList = farmsMapper.selectByExample(example);


        PageInfo<Farms> pageInfo = new PageInfo<Farms>(farmsList);
        return pageInfo;


    }

    @Override
    public List<Farms> querySelectFarmList(Integer farmType, String farmsName) {
        Example example = new Example(Farms.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("farmType", farmType);

        if (!StringUtil.isBlank(farmsName)) {

            criteria.andLike("farmsname", "%" + farmsName + "%");
        }

        List<Farms> farmsList = farmsMapper.selectByExample(example);

        return farmsList;

    }

    @Override
    public List<Farms> selectByFarmIds(List<Long> ids) {
        return farmsMapper.selectByFarmIds(ids);
    }

    @Override
    public Farms getFarmsById(Long id) {
        return farmsMapper.selectByPrimaryKey(id);

    }

    /**
     * 根据用户id查询其管辖区域范围下的养殖场（只是规模场）
     *
     * @param userId
     * @return
     */
    @Override
    public List<Farms> selectByUserId(Long userId) {
        return farmsMapper.selectByUserId(userId);
    }

    /**
     * 根据区域编号和当前登陆人查询其区域范围内的场
     *
     * @param provinceId
     * @param cityId
     * @param regionId
     * @param streetId
     * @param villageId
     */

    @Override
    public List<Farms> selectByArea(Long userId, Integer provinceId, Integer cityId, Integer regionId, Integer streetId, Integer villageId, Integer farmType) {
        if (userId == null) {
            throw new OPServerException("用户未登录,请登录后重试");
        }
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        if (provinceId != null) {
            map.put("provinceId", provinceId);
        }
        if (cityId != null) {
            map.put("cityId", cityId);
        }
        if (regionId != null) {
            map.put("regionId", regionId);
        }
        if (streetId != null) {
            map.put("streetId", streetId);
        }
        if (villageId != null) {
            map.put("villageId", villageId);
        }
        return farmsMapper.selectByMap(map);
    }

    /**
     * 根据用户id,条件分页查场
     *
     * @param pageNum
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public PageInfo<Farms> selectByUserIdPage(Integer pageNum, Integer pageSize, Map map) {
        Long userId = (Long) map.get("userId");
        if (userId == null) {
            throw new OPServerException("用户未登录,请登陆后重试");
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Farms> farmsList = farmsMapper.selectByMap(map);
        return new PageInfo<Farms>(farmsList);
    }

    @Override
    public List<Farms> selectByMap(Map map) {
        Long userId = (Long) map.get("userId");
        if (userId == null) {
            throw new OPServerException("用户未登录,请登陆后重试");
        }

        List<Farms> farmsList = farmsMapper.selectByMap(map);
        return farmsList;
    }

    @Override
    public List<Farms> findFarmByAreaForV(Map<String, Object> map) {
        return farmsMapper.findFarmByAreaForV(map);
    }

    /**
     * 根据政府用户id,条件分页查询养殖场(只是规模场)
     *
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public PageInfo<Farms> selectByUserId(Integer pageNum, Integer pageSize, Long userId) {

        PageHelper.startPage(pageNum, pageSize);
        List<Farms> farmsList = farmsMapper.selectByUserId(userId);
        return new PageInfo<Farms>(farmsList);

    }

    @Override
    public Map<Integer, Integer> selectTotalQuantity(Long userId, List<Integer> farmTypes, List<Integer> animalTwoTypes, List<Integer> areaList) {
        Map<Integer, Integer> result = Maps.newHashMap();
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        if (farmTypes.size() > 0) {
            map.put("farmTypes", farmTypes);
        }
        if (areaList != null && areaList.size() > 0) {
            switch (areaList.size()) {
                case 1:
                    map.put("provinceId", areaList.get(0));
                    break;
                case 2:
                    map.put("provinceId", areaList.get(0));
                    map.put("cityId", areaList.get(1));
                    break;
                case 3:
                    map.put("provinceId", areaList.get(0));
                    map.put("cityId", areaList.get(1));
                    map.put("regionId", areaList.get(2));
                    break;
                case 4:
                    map.put("provinceId", areaList.get(0));
                    map.put("cityId", areaList.get(1));
                    map.put("regionId", areaList.get(2));
                    map.put("streetId", areaList.get(3));
                    break;
                case 5:
                    map.put("provinceId", areaList.get(0));
                    map.put("cityId", areaList.get(1));
                    map.put("regionId", areaList.get(2));
                    map.put("streetId", areaList.get(3));
                    map.put("villageId", areaList.get(4));
                    break;
                default:
                    break;
            }
        }
        if (animalTwoTypes.size() > 0) {
            for (Integer animalTwoType : animalTwoTypes) {
                map.put("animalTwoType", animalTwoType);
                Integer quantity = farmStockService.selectTotalQuantity(map);
                result.put(animalTwoType, quantity);
            }
        }

        return result;
    }

    /**
     * 带存栏查询
     *
     * @param map
     * @return
     */
    @Override
    public List<FarmsDto> selectFarmStock(Map map) {

        return farmsMapper.selectFarmStock(map);
    }

    @Override
    public PageInfo<FarmsDto> selectFarmStock(Integer pageNum, Integer pageSize, Map map) {
        PageHelper.startPage(pageNum, pageSize);

        return new PageInfo<>(farmsMapper.selectFarmStock(map));
    }

    @Override
    public PageInfo<String> selectFarmVillage(Integer pageNum, Integer pageSize, Map map) {
        PageHelper.startPage(pageNum, pageSize);
        List<String> villageList = farmsMapper.selectFarmVillage(map);
        return new PageInfo<>(villageList);
    }

    /**
     * 政府人员根据其管辖区域查询区域内已经养殖的畜种列表(二级畜种)
     * @param userId
     * @return 畜种code list
     */
    @Override
    public List<Integer> selectFarmBreedTypeByGovUserId(Long userId) {
        Map<String,Object> map = Maps.newHashMap();
        map.put("userId",userId);

        //指定养殖场类型是规模场和散养场
        List<Integer> farmsTypeList = Lists.newArrayList(FarmsEnum.Breed.getCode(),FarmsEnum.Retail.getCode());
        map.put("farmsTypes",farmsTypeList);

        map.put("status",FarmsEnum.status_true.getCode());

        List<Integer> animalCodeList = farmsMapper.selectFarmBreedTypeByGovUserId(map);

        return animalCodeList;
    }

    /**
     * 牛档案政府端 -- 养殖场列表 (养牛的养殖场)
     */
    @Override
    public List<Farms> selectFarmListByAnimalType(Long userId, Integer breedSecondType) {

        Map<String,Object> map = Maps.newHashMap();

        map.put("userId",userId);
        map.put("animalTwoType",breedSecondType);

        List<Farms> farmsList = farmsMapper.selectByMap(map);

        return farmsList;
    }
}